//
//  MainTabBarController.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/8/23.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {
    
    var centerIcon:CenterTabbar!
    var publishView:PublishView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.createVCtoTabBar()
        
        //自定义的tabbar
        centerIcon = CenterTabbar()
        self.setValue(centerIcon, forKey: "tabBar")
        centerIcon.centerIcon.addTarget(self, action: #selector(self.centerIconClick(_:)), forControlEvents: .TouchUpInside)
        
        //不透明 设置黑色
        self.tabBar.barTintColor = UIColor.blackColor()
        self.tabBar.translucent = false
        
        //去掉上方衡线
        self.tabBar.shadowImage = UIImage()
        self.tabBar.backgroundImage = UIImage()
        
    }
    
    //创建tabbar的分试图
    func createVCtoTabBar(){
        self.addTabBarItemViewC(HomeViewController(), titleName: "首页", grayImageName: "homeGray", selectorImageName: "home")
        self.addTabBarItemViewC(MyViewController(), titleName: "我的", grayImageName: "myGray", selectorImageName: "my")
    }
    
    //封装创建tabbarItem的方法
    func addTabBarItemViewC(vc:UIViewController,titleName:String,grayImageName:String,selectorImageName:String){
        //因为系统默认图片会变为蓝色，我们创建自己想要的颜色的图片
        let itemImg = UIImage(named: selectorImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        let itemImgGray = UIImage(named: grayImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

        vc.tabBarItem = UITabBarItem(title: titleName, image: itemImgGray, selectedImage: itemImg)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:Theme.YellowColor,NSFontAttributeName:UIFont.systemFontOfSize(9)], forState: UIControlState.Selected)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:Theme.GrayColor,NSFontAttributeName:UIFont.systemFontOfSize(9)], forState: UIControlState())
        
        let navigationView = MainNavigationController(rootViewController: vc)
        
        self.addChildViewController(navigationView)
    }
}

//MARK:--中间按钮的点击事件 动画等相关的方法
extension MainTabBarController{
    //中间按钮的点击事件
    func centerIconClick(sender:UIButton){
        publishView = PublishView()
        self.addChildViewController(publishView)
        publishView.didMoveToParentViewController(self)
        publishView.view.frame = MainBounds
        self.view.addSubview(publishView.view)
        
    }
    
    //按钮点击后的动画
    func animateForCenterIcon(){
        UIView.animateWithDuration(0.2, delay: 0, options: .CurveEaseOut, animations: {
            self.publishView.view.alpha = 1
            }, completion: nil)
    }
}













